﻿using System;
using System.Runtime.InteropServices;

namespace SpiderMonkey
{
    /// <summary>
    /// Describes the format of a JS error that is used either by the internal error reporting mechanism or by a user-defined error-reporting mechanism.
    /// </summary>
    public sealed class ErrorReport : NativeObject
    {
        internal ErrorReport(IntPtr ptr)
            : base(ptr)
        {
        }

        protected override void Dispose(bool disposing)
        {
        }
        
        /// <summary>
        /// Indicates the source file or URL that produced the error condition. If NULL, the error is local to the script in the current HTML page.
        /// </summary>
        public string FileName
        {
            get
            {
                IntPtr stringPointer = Marshal.ReadIntPtr(Pointer, 0);
                if (stringPointer == IntPtr.Zero) return null;
                return Marshal.PtrToStringAnsi(stringPointer);
            }
        }
        /// <summary>
        /// Line number in the source that caused the error.
        /// </summary>
        public uint LineNumber
        {
            get { return (uint)Marshal.ReadInt32(Pointer, IntPtr.Size); }
        }
        /// <summary>
        /// Text of the line that caused the error, minus the trailing newline character.
        /// </summary>
        public string Line
        {
            get
            {
                IntPtr stringPointer = Marshal.ReadIntPtr(Pointer, IntPtr.Size + sizeof(uint));
                if (stringPointer == IntPtr.Zero) return null;
                return Marshal.PtrToStringAnsi(stringPointer);
            }
        }
        /// <summary>
        /// Pointer to the error token in *linebuf.
        /// </summary>
        public string Token
        {
            get
            {
                IntPtr stringPointer = Marshal.ReadIntPtr(Pointer, IntPtr.Size * 2 + sizeof(uint));
                if (stringPointer == IntPtr.Zero) return null;
                return Marshal.PtrToStringAnsi(stringPointer);
            }
        }
        /// <summary>
        /// Unicode line buffer. This is the buffer that contains the original data.
        /// </summary>
        public string UnicodeLine
        {
            get { throw new NotImplementedException(); }
        }
        /// <summary>
        /// Pointer to the error token in *uclinebuf.
        /// </summary>
        public string UnicodeToken
        {
            get { throw new NotImplementedException(); }
        }
        public Report Flags
        {
            get { return (Report)Marshal.ReadInt32(Pointer, IntPtr.Size * 5 + sizeof(uint)); }
        }
        /// <summary>
        /// The error number.
        /// </summary>
        public uint ErrorNumber
        {
            get { return (uint)Marshal.ReadInt32(Pointer, IntPtr.Size * 5 + sizeof(uint) * 2); }
        }
        /// <summary>
        /// The default unicode error message.
        /// </summary>
        public string UnicodeMessage
        {
            get { throw new NotImplementedException(); }
        }
        /// <summary>
        /// Arguments for the error message.
        /// </summary>
        public string[] MessageArgs
        {
            get { throw new NotImplementedException(); }
        }
    }
}
